
package com.rohoo.framework.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.rohoo.framework.util.sequence.Sequence;
/**
 * 结果分页显示页面排序标签
 * @author futianqi
 *
 */
public class OrderTag extends TagSupport {
	private String field;
	private String sid;
	private String css;

	private static int SORT_NONE = 0;
	private static int SORT_ASC = 1;
	private static int SORT_DESC = 2;
	
	public void setField(String field) {
		this.field = field;
	}

	public int doStartTag() throws JspException {
		StringBuffer html = new StringBuffer();

		try {
			sid = Sequence.getSequence();
		} catch (Exception ex) {
			throw new JspException(ex.getMessage());
		}

		html.append("<a id='order" + sid + "' style='cursor:hand' ");
		if (css != null && css.trim().length() > 0)
			html.append("class=" + css);
		html.append(">");

		try {
			pageContext.getOut().print(html.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return EVAL_BODY_INCLUDE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.jsp.tagext.Tag#doEndTag()
	 */
	public int doEndTag() throws JspException {
		StringBuffer html = new StringBuffer();

		HttpServletRequest request = null;
		if (pageContext.getRequest() instanceof HttpServletRequest) {
			request = (HttpServletRequest) pageContext.getRequest();
		}
		
		String _orderby = "";
		if(request.getParameter("_orderby")==null){
			_orderby = "";
		}else{
			_orderby = (String)request.getParameter("_orderby");
		}
		

		html.append("</a>");
 
		int sort = getSort();
		if(_orderby.indexOf(field) >=0){
			if (sort == SORT_ASC) {
				html.append("<img src='" + request.getContextPath()
							+ "/resources/images/up.gif' title='升序'>");
			}
	
			if (sort == SORT_DESC) {
					html.append("<img src='" + request.getContextPath()
							+ "/resources/images/down.gif' title='降序'>");
			}
		}

		html.append("<input id='orderby" + sid
				+ "' type='hidden' name='_orderby1' value='"
				+ getCurrentOrder() + "'>");
		html.append("<script>document.all('order"
						+ sid
						+ "').onclick=new Function(\"document.all('orderby"
						+ sid
						+ "').value=\'"
						+ getNextOrder()
						+ "\';document.all('_orderby').value=\'" 
						+ getNextOrder()
						+ "\';document.formList.submit();\")</script>");

		try {
			pageContext.getOut().print(html.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * Get the sort Type.
	 * 
	 * @return The sort Type.
	 */
	private int getSort() {
		HttpServletRequest request = (HttpServletRequest) pageContext
				.getRequest();

		String[] orderbys = request.getParameterValues("_orderby1");
		int sort = SORT_NONE;

		for (int i = 0; orderbys != null && i < orderbys.length; i++) {
			String orderby = orderbys[i];
			if (orderby == null || orderby.equals(""))
				continue;

			if (orderby.trim().equalsIgnoreCase(field)) {
				sort = SORT_ASC;
			} else if (orderby.trim().equalsIgnoreCase(field + " desc")) {
				sort = SORT_DESC;
			}
		}
		
		return sort;
	}

	/**
	 * Get the current order.
	 * 
	 * @return The current order.
	 */
	private String getCurrentOrder() {
		int sort = getSort();
		String order = "";

		if (sort == SORT_NONE)
			order = "";

		if (sort == SORT_ASC)
			order = field;

		if (sort == SORT_DESC)
			order = field + " desc";
		return order;
	}

	/**
	 * Get the next order
	 * 
	 * @return The next order.
	 */
	private String getNextOrder() {
		int sort = getSort();
		String order = "";

		if (sort == SORT_NONE)
			order = field;

		if (sort == SORT_ASC)
			order = field + " desc";

		if (sort == SORT_DESC)
			order = "";
return order;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}

}
